package com.fau.hm.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.validation.ConstraintViolation;

import org.hibernate.Hibernate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.fau.hm.entity.MedicalAdmin;
import com.fau.hm.entity.MedicalResource;
import com.fau.hm.entity.MedicalRole;
import com.fau.hm.service.IRoleManagerService;
import com.fau.hm.util.Page;
import com.fau.hm.util.ParamUtil;
import com.fau.hm.util.SystemConfig;
import com.fau.hm.util.ValidatorUtil;

@Service
@Transactional(readOnly = true)
public class RoleManagerService extends BaseService implements IRoleManagerService {

	@Override
	public Page findRoleList(Map<String, Object> map, MedicalRole role, MedicalAdmin admin) {
		StringBuilder statusSql = new StringBuilder("from MedicalRole a where 1=1 and a.eid = ? and a.id <> ? ");
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalRole a where 1=1 and a.eid = ? and a.id <> ? ");
//		StringBuilder statusSql = new StringBuilder("from MedicalRole a where 1=1 and a.eid = ? ");
//		StringBuilder countSql = new StringBuilder("select count(*) from MedicalRole a where 1=1 and a.eid = ? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getEnterpriseId());
		objectList.add(SystemConfig.SUPER_ID);
		
		statusSql.append("order by a.createTime desc ");
		return this.iBaseDao.getPage(ParamUtil.getInteger(map,"pageIndex",1),ParamUtil.getInteger(map, "pageSize", 20),
				map, countSql.toString(), statusSql.toString(), objectList);
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean saveRole(MedicalRole role, MedicalAdmin admin) {
		role.setEid(admin.getEnterpriseId());
		Set<ConstraintViolation<Object>> errorInfo = ValidatorUtil.validateObj(role);
		if(errorInfo.size() > 0) {
			return false;
		} else {
			return this.iBaseDao.saveObject(role);
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean saveEditRole(MedicalRole role) {
		MedicalRole bean = (MedicalRole) this.iBaseDao.getObject(MedicalRole.class, role.getId());
		bean.setName(role.getName());
		bean.setDescription(role.getDescription());
		Set<ConstraintViolation<Object>> errorInfo = ValidatorUtil.validateObj(bean);
		if(errorInfo.size() > 0) {
			return false;
		} else {
			return this.iBaseDao.saveObject(bean);
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void delRole(MedicalRole role) {
		this.iBaseDao.deleteObject(role);
	}

	@Override
	public boolean includeUser(String roleId) {
		MedicalRole role = (MedicalRole) this.iBaseDao.getObject(MedicalRole.class, roleId);
		Hibernate.initialize(role.getAdminSet());
		if(role.getAdminSet().size() > 0){
			return true;
		}else{
			return false;
		}
	}

	@SuppressWarnings("all")
	@Override
	public List<MedicalResource> findAllResource() {
		List<MedicalResource> list = this.iBaseDao.getBySql("from MedicalResource m order by m.orderNum asc ", null, null);
		return list;
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean saveRoleResource(MedicalRole role, String resourceIds) {
		String[] str = resourceIds.split(",");
		role = (MedicalRole) this.iBaseDao.getObject(MedicalRole.class, role.getId());
		role.getResourceSet().clear();
		
		boolean cmFlag = true;
		boolean omFlag = true;
		boolean wmFlag = true;
		boolean mmFlag = true;
		boolean fmFlag = true;
		boolean hmFlag = true;
		boolean ssFlag = true;
		
		for (String rid : str) {
			MedicalResource bean = (MedicalResource) this.iBaseDao.getObject(MedicalResource.class, rid);
			if(SystemConfig.CM_SYSTEM_TYPE.equals(bean.getTypeName()) && cmFlag) {
				MedicalResource resource = new MedicalResource();
				resource.setId("2");
				role.getResourceSet().add(resource);
				cmFlag = false;
			} else if(SystemConfig.OM_SYSTEM_TYPE.equals(bean.getTypeName()) && omFlag) {
				MedicalResource resource = new MedicalResource();
				resource.setId("1");
				role.getResourceSet().add(resource);
				omFlag = false;
			} else if(SystemConfig.WM_SYSTEM_TYPE.equals(bean.getTypeName()) && wmFlag) {
				MedicalResource resource = new MedicalResource();
				resource.setId("0");
				role.getResourceSet().add(resource);
				wmFlag = false;
			} else if(SystemConfig.MM_SYSTEM_TYPE.equals(bean.getTypeName()) && mmFlag) {
				MedicalResource resource = new MedicalResource();
				resource.setId("4");
				role.getResourceSet().add(resource);
				mmFlag = false;
			} else if(SystemConfig.FM_SYSTEM_TYPE.equals(bean.getTypeName()) && fmFlag) {
				MedicalResource resource = new MedicalResource();
				resource.setId("3");
				role.getResourceSet().add(resource);
				fmFlag = false;
			} else if(SystemConfig.HM_SYSTEM_TYPE.equals(bean.getTypeName()) && hmFlag) {
				MedicalResource resource = new MedicalResource();
				resource.setId("5");
				role.getResourceSet().add(resource);
				hmFlag = false;
			} else if(SystemConfig.SS_SYSTEM_TYPE.equals(bean.getTypeName()) && ssFlag) {
				MedicalResource resource = new MedicalResource();
				resource.setId("6");
				role.getResourceSet().add(resource);
				ssFlag = false;
			}
			MedicalResource resource = new MedicalResource();
			resource.setId(rid);
			role.getResourceSet().add(resource);
		}
		boolean falg = this.iBaseDao.saveObject(role);
		return falg;
	}

	@Override
	public List<String> findByRoleId(String roleId) {
		List<String> list = new ArrayList<String>();
		MedicalRole role = (MedicalRole) this.iBaseDao.getObject(MedicalRole.class, roleId);
		Hibernate.initialize(role.getResourceSet());
		for(Iterator<MedicalResource> it = role.getResourceSet().iterator(); it.hasNext();) {
			list.add(it.next().getId());
		}
		return list;
	}

	@Override
	public boolean includeRole(String eid, String name) {
		int count = this.iBaseDao.getIntCount("select count(*) from MedicalRole m where m.eid = ? and m.name = ? ", eid, name);
		if(count > 0){
			return true;
		}else{
			return false;
		}
	}

}
